//
//  IFlyManager.swift
//  MMStudent
//
//  Created by Mac on 2018/4/19.
//  Copyright © 2018年 RanDian. All rights reserved.
//

import UIKit
import iflyMSC

@objc public protocol IFlyManagerDelegate {
    @objc func result(resultArray: [Any], isLast: Bool)
}

public class IFlyManager: NSObject {
    
    public var iflyRecognizerView: IFlyRecognizerView!
    
    public var speechPlayer: AVSpeechSynthesizer!
    public var utterance: AVSpeechUtterance!
    
    public weak var delegate: IFlyManagerDelegate?
    
    public func initIFly(point: CGPoint) {
        if iflyRecognizerView != nil {
            iflyRecognizerView.setParameter(IFLY_AUDIO_SOURCE_MIC, forKey: "audio_source")
            iflyRecognizerView.setParameter("plain", forKey: IFlySpeechConstant.result_TYPE())
            iflyRecognizerView.setParameter("asr.pcm", forKey: IFlySpeechConstant.asr_AUDIO_PATH())
            return
        }
        iflyRecognizerView = IFlyRecognizerView(center: point)
        iflyRecognizerView.setAutoRotate(true)
        iflyRecognizerView.setParameter("", forKey: IFlySpeechConstant.params())
        iflyRecognizerView.setParameter("iat", forKey: IFlySpeechConstant.ifly_DOMAIN())
        let config = IATConfig.sharedConfig
        config.language = IATConfig.english()
        //设置最长录音时间
        iflyRecognizerView.setParameter(config.speechTimeout, forKey: IFlySpeechConstant.speech_TIMEOUT())
        //设置前端点
        iflyRecognizerView.setParameter(config.vadBos, forKey: IFlySpeechConstant.vad_BOS())
        //设置后端点
        iflyRecognizerView.setParameter(config.vadEos, forKey: IFlySpeechConstant.vad_EOS())
        //网络等待时间
        iflyRecognizerView.setParameter("5000", forKey: IFlySpeechConstant.net_TIMEOUT())
        //设置采样率，推荐使用16K
        iflyRecognizerView.setParameter(config.sampleRate, forKey: IFlySpeechConstant.sample_RATE())
        
        if config.language == IATConfig.chinese() {
            iflyRecognizerView.setParameter(config.accent, forKey: IFlySpeechConstant.accent())
        }
        
        iflyRecognizerView.setParameter(config.language, forKey: IFlySpeechConstant.language())
        iflyRecognizerView.setParameter(config.dot, forKey: IFlySpeechConstant.asr_PTT())
        
        iflyRecognizerView.setParameter(IFLY_AUDIO_SOURCE_MIC, forKey: "audio_source")
        iflyRecognizerView.setParameter("plain", forKey: IFlySpeechConstant.result_TYPE())
        iflyRecognizerView.setParameter("asr.pcm", forKey: IFlySpeechConstant.asr_AUDIO_PATH())
        
        iflyRecognizerView.delegate = self
    }
    
    public func initSpeechPlayer() {
        speechPlayer = AVSpeechSynthesizer()
    }
    
    public func startSpeech(content: String?) {
        guard let content = content else {
            return
        }
        
        utterance = AVSpeechUtterance(string: content)
        utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
        utterance.rate = 0.5
        utterance.volume = 1.0
        utterance.pitchMultiplier = 1.0
        utterance.postUtteranceDelay = 0
        speechPlayer.speak(utterance)
    }
    
    public func start() {
        iflyRecognizerView.start()
    }
}

extension IFlyManager: IFlyRecognizerViewDelegate {
    public func onResult(_ resultArray: [Any]!, isLast: Bool) {
        delegate?.result(resultArray: resultArray, isLast: isLast)
    }
    
    public func onError(_ error: IFlySpeechError!) {
        
    }
}


